#include "head.h"

int server_register(int net_fd,self_defined_pro_t *mes){

     self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    char *username=strtok(mes->message,"=");
    username=strtok(NULL,"&");
    char *password=strtok(NULL,"=");
    password=strtok(NULL,"&");

    char path[60]={0};
    strcpy(path,load_config().file);
    DIR *dirp=opendir(path);
    struct dirent *pdirent;
    while((pdirent=readdir(dirp))!=NULL){
        struct stat stat_buf;
        stat(pdirent->d_name,&stat_buf);
        if(strcmp(pdirent->d_name,username)==0){
            send_mes->cmd=400;
            strcpy(send_mes->message,"register failed!");
            send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);
            return 0;
        }
    }
    strcat(path,"/");
    strcat(path,username);
    mkdir(path,0777);
    send_mes->cmd=200;
    send_mes->message_len=6;
    send_mes->para_num=1;
    strcpy(send_mes->message,"注册成功！\n");
    send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

    return 0;
}

int server_register2(int net_fd,self_defined_pro_t *mes){

    char *path=strtok(mes->message,"=");
    path=strtok(NULL,"&");
    char *username=strtok(NULL,"=");
    username=strtok(NULL,"&");
    char *password=strtok(NULL,"=");
    password=strtok(NULL,"&");

    // TODO: 判断路径是否存在
    //       使用配置文件的基本路径 +拼接 用户名, 检测这个目录是否存在
    //       如果存在, 不允许注册重复账号

    // 走到这, 意味着账号不存在: 可以创建
    self_defined_pro_t *send_mes=(self_defined_pro_t *)calloc(1,sizeof(self_defined_pro_t));
    mkdir(path,0777);
    send_mes->cmd=200;
    send_mes->message_len=6;
    send_mes->para_num=1;
    strcpy(send_mes->message,"注册成功！\n");
    send(net_fd,send_mes,sizeof(self_defined_pro_t),MSG_NOSIGNAL);

    return 0;
}
